import pygame
import random
from pygame.sprite import Sprite


class CatchRole(Sprite):
    def __init__(self, screen):
        super(CatchRole, self).__init__()
        self.screen = screen

        # 存储角色
        self.screen_rect = screen.get_rect()
        self.rect = pygame.Rect(0, 0, 50, 5)
        self.rect.x = random.randint(self.rect.left,self.screen_rect.right)
        self.rect.y = self.screen_rect.bottom - 15

        # 角色移动速度
        self.x = float(self.rect.x)
        self.role_color = 60, 60, 60
        self.speed_factor = random.randint(1,3)
        self.moving_left = False
        self.moving_right = False

    def update(self):
        """左右移动角色"""
        # 更新飞船的center而不是rect值
        if self.moving_right and self.screen_rect.right > self.rect.right:
            self.x += self.speed_factor
        if self.moving_left and self.rect.left > 0:
            self.x -= self.speed_factor
        # 根据self.center更新rect对象
        self.rect.x = int(self.x)

    def draw_catch_role(self):
        """在屏幕上绘制子弹"""
        pygame.draw.rect(self.screen,self.role_color,self.rect)
